package com.ecube.mqtt.service;

import com.ecube.mqtt.MqttUtils;
import com.ecube.mqtt.mysql.Query;
import org.junit.Test;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class PileChargeSendService {


    public Boolean startFail(String env, String chgID, String gunNo, String result, String reson) throws InterruptedException {

//        //非常变参数
//        String env = "UAT";
//        String gunNo = "1";
//        String chgID = "1100932023011800";
//        String vin = "Yh821214354323424";
        if (gunNo.equals("1,2")){
            gunNo="1";
        }
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long t = date.getTime() - 5*60*1000;
        String starttime = dateFormat.format(t);
        String orderSn = Query.getOrderSN(env, chgID, starttime);
        String index = Query.getSnno(env, chgID);

        //启动充电
        Boolean sendStart = MqttUtils.sendStartFailPileChargeMQTT(env, orderSn, index, chgID, gunNo, result, reson, dateFormat.format(date));


        if(sendStart){
            System.out.println("充电发送成功");
        }else {
            System.out.println("充电发送失败");
        }
        return sendStart;
    }

    public Boolean start(String env, String chgID, String gunNo, String plateNo, String vin) throws InterruptedException {

//        //非常变参数
//        String env = "UAT";
//        String gunNo = "1";
//        String chgID = "1100932023011800";
//        String vin = "Yh821214354323424";
        if (gunNo.equals("1,2")){
            gunNo="1";
        }
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long t = date.getTime() - 5*60*1000;
        String starttime = dateFormat.format(t);
        String orderSn = Query.getOrderSN(env, chgID, starttime);
        String index = Query.getSnno(env, chgID);

        //启动充电
        Boolean sendStart = MqttUtils.sendStartPileChargeMQTT(env, orderSn, index, chgID, gunNo, dateFormat.format(date));


        if(sendStart){
            System.out.println("充电启动成功");
        }else {
            System.out.println("充电启动失败");
        }
        return sendStart;
    }


    public Boolean bms(String env, String chgID, String gunNo, String vin) throws InterruptedException {

//        //非常变参数
//        String env = "UAT";
//        String gunNo = "1";
//        String chgID = "1100932023011800";
//        String vin = "Yh821214354323424";
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long t = date.getTime() - 5*60*1000;
        String starttime = dateFormat.format(t);
        String orderSn = Query.getOrderSN(env, chgID, starttime);
        String index = Query.getSnno(env, chgID);


        //bms报文
        Boolean sendBMS = MqttUtils.sendBMS(env, orderSn, index, chgID, vin, dateFormat.format(date));

        if(sendBMS){
            System.out.println("BMS发送成功");
        }else {
            System.out.println("BMS发送失败");
        }
        return sendBMS;
    }

    //鉴权
    public Boolean auth(String env, String chgID, String gunNo, String vin) throws InterruptedException {

//        //非常变参数
//        String env = "UAT";
//        String gunNo = "1";
//        String chgID = "1100932023011800";
//        String vin = "Yh821214354323424";
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long t = date.getTime() - 5*60*1000;
        String starttime = dateFormat.format(t);
        String orderSn = Query.getOrderSN(env, chgID,starttime);
        String index = Query.getSnno(env, chgID);

        if (gunNo.equals("1,2")){
            gunNo="1";
        }
        //鉴权
        Boolean authentication = MqttUtils.sendAuthenticationMQTT(env, orderSn, index, chgID, gunNo, vin, dateFormat.format(date));

        if(authentication){
            System.out.println("鉴权发送成功");
        }else {
            System.out.println("鉴权发送失败");
        }
        return authentication;
    }

    //开始事件
    public Boolean startEvent(String env, String chgID, String gunNo, String plateNo, String vin) throws InterruptedException {

//        //非常变参数
//        String env = "UAT";
//        String gunNo = "1";
//        String chgID = "1100932023011800";
//        String vin = "Yh821214354323424";
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long t = date.getTime() - 5*60*1000;
        String starttime = dateFormat.format(t);
        String orderSn = Query.getOrderSN(env, chgID, starttime);
        String index = Query.getSnno(env, chgID);

        //启动充电

        Boolean startEvent = MqttUtils.sendPileStartMQTT(env, orderSn, index, chgID, dateFormat.format(date));

        //Thread.sleep(2000);

        if(startEvent){
            System.out.println("充电事件-开始发送成功");
        }else {
            System.out.println("充电事件-开始发送失败");
        }
        return startEvent;
    }
    //充电桩实时状态
    public Boolean state(String env, String chgID, String gunNo, String vin, String soc) throws InterruptedException {
        //常变参数
//        String soc = "70";
//        //非常变参数
//        String env = "UAT";
//        String gunNo = "1";
//        String chgID = "1100932023011800";
//        String vin = "Yh821214354323424";

        String index = Query.getSnno(env, chgID);

        //实时状态
        Boolean sendState = MqttUtils.sendPileStateMQTT(env, soc, index, chgID, vin, gunNo);

        if(sendState){
            System.out.println("实时状态更新成功");
        }else {
            System.out.println("实时状态更新失败");
        }
        return sendState;
    }


    //充电桩结束充电
    public Boolean stop(String env, String chgID, String gunNo) throws InterruptedException {
//
//        //非常变参数
//        String env = "UAT";
//        String gunNo = "1";
//        String chgID = "1100932023011800";
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long t = date.getTime() - 5*60*1000;
        String starttime = dateFormat.format(t);
        String orderSn = Query.getOrderSN(env, chgID, starttime);
        String index = Query.getSnno(env, chgID);
        if (gunNo.equals("1,2")){
            gunNo="1";
        }
        //结束充电
        Boolean sendStop = MqttUtils.sendStopPileChargeMQTT(env, orderSn, index, chgID, gunNo, dateFormat.format(date));
        //Thread.sleep(5000);

        if(sendStop){
            System.out.println("充电结束成功");
        }else {
            System.out.println("充电结束失败");
        }
         return sendStop;
    }

    //充电事件-结束
    public Boolean stopEvent(String env, String chgID, String gunNo) throws InterruptedException {
//
//        //非常变参数
//        String env = "UAT";
//        String gunNo = "1";
//        String chgID = "1100932023011800";
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long t = date.getTime() - 5*60*1000;
        String starttime = dateFormat.format(t);
        String orderSn = Query.getOrderSN(env, chgID, starttime);
        String index = Query.getSnno(env, chgID);


        //充电事件-停止
        Boolean sendStopEvent = MqttUtils.sendStopPileEventChargeMQTT(env, orderSn, index, chgID, dateFormat.format(date));

        if(sendStopEvent){
            System.out.println("充电事件-停止发送成功");
        }else {
            System.out.println("充电事件-停止发送失败");
        }
        return sendStopEvent;
    }

    public String getEventJson(String env, String chgID, String gunNo, String vin) throws InterruptedException {

//        //非常变参数
//        String env = "UAT";
//        String gunNo = "1";
//        String chgID = "1100932023011800";
//        String vin = "Yh821214354323424";

        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long t = date.getTime() - 5*60*1000;
        String starttime = dateFormat.format(t);
        String rateModelID = Query.getSwapChargeModelId(env, chgID);
        String orderSn = Query.getOrderSN(env, chgID, starttime);
        String index = Query.getSnno(env, chgID);

        //获取充电事件json
         return MqttUtils.getEventJson(env, orderSn, index, chgID, vin, gunNo, rateModelID);


    }

    public Boolean sendEvent(String env, String chgID, String gunNo, String vin, String time) throws InterruptedException {

//        //非常变参数
//        String env = "UAT";
//        String gunNo = "1";
//        String chgID = "1100932023011800";
//        String vin = "Yh821214354323424";

        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long t = date.getTime() - 5*60*1000;
        String starttime = dateFormat.format(t);

        String rateModelID = Query.getSwapChargeModelId(env, chgID);
        String orderSn = Query.getOrderSN(env, chgID, starttime);
        if (orderSn.equals("")||orderSn.isEmpty()) {
            orderSn =SwapSendService.getRandom2(15);
            }
        String index = Query.getSnno(env, chgID);

        //获取充电事件json
        //MqttUtils.getEventJson(env, orderSn, index, chgID, vin, gunNo, rateModelID);

        //发送充电事件
        Boolean sendEvent = MqttUtils.sendPileEventMQTT(env, orderSn, index, chgID, vin, gunNo, rateModelID, time);
        if(sendEvent){
            System.out.println("充电记录发送成功");
        }else {
            System.out.println("充电记录发送失败");
        }
        return sendEvent;
    }

    public Boolean chgState(String env, String chgID, String gunNo, String spotstate, String stateA, String stateB) throws InterruptedException {

//        //非常变参数
//        String env = "UAT";
//        String gunNo = "1";
//        String chgID = "1100932023011800";
//        String vin = "Yh821214354323424";
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String orderSn = Query.getOrderSN(env, chgID);
//        String index = Query.getSnno(env, chgID);

        //启动充电
        Boolean sendStart = MqttUtils.sendChgStateMQTT(env, chgID, spotstate, stateA, stateB, dateFormat.format(date));


        if(sendStart){
            System.out.println("充电枪状态发送成功");
        }else {
            System.out.println("充电枪状态发送失败");
        }
        return sendStart;
    }

    public Boolean rate(String env, String chgID) throws InterruptedException {

//        //非常变参数
//        String env = "UAT";
//        String gunNo = "1";
//        String chgID = "1100932023011800";
//        String vin = "Yh821214354323424";
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String orderSn = Query.getOrderSN(env, chgID);
//        String index = Query.getSnno(env, chgID);

        //启动充电
        Boolean rate = MqttUtils.sendRateMQTT(env, chgID, dateFormat.format(date));


        if(rate){
            System.out.println("费率下发成功");
        }else {
            System.out.println("费率下发失败");
        }
        return rate;
    }

    public static void main(String[] args) {
        String index = Query.getSnno("UAT", "1110001111119999");
        MqttUtils.sendPileStateMQTT("UAT", "19", index, "1110001111119999", "YH932438187721111", "1,2");
    }

}
